package com.diankangwo.controller;

import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.BrowseLogDAO;
import com.diankangwo.dao.CommodityDAO;
import com.diankangwo.dao.UserDAO;
import com.diankangwo.domain.BrowseLog;
import com.diankangwo.domain.Commodity;
import com.diankangwo.domain.UserInfo;
import com.diankangwo.util.PageSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

/**
 * 商品控制器
 *
 * @author xiaoyu
 */
@Controller
@Api(description = "商品")
public class CommodityController {

    @Autowired
    private UserDAO userDao;

    @Autowired
    private CommodityDAO commodityDao;

    @Autowired
    private BrowseLogDAO browseLogDAO;

    /**
     * 获取商品列表接口
     *
     * @param primaryClassificationID   一级分类ID
     * @param secondaryClassificationID 二级分类ID
     * @param thridClassificationID     二级分类ID
     * @param searchKey                 搜索关键词
     * @param sortOrder                 排序方式
     * @param priceMax                  最高价,仅当sort_order = price_max_min 时生效
     * @param priceMin                  最低价,仅当sort_order = price_max_min 时生效
     * @param globlFieldId              全球购场馆ID
     * @return
     */
    @ApiOperation(value = " 获取商品列表接口", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "primaryClassificationID", value = "一级分类ID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "secondaryClassificationID", value = "二级分类ID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "thridClassificationID", value = "三级分类ID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "searchKey", value = "搜索关键词", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortOrder", value = "排序方式", required = false, dataType = "String"),
            @ApiImplicitParam(name = "priceMax", value = "最高价,仅当sort_order = price_max_min 时生效", required = false, dataType = "Float"),
            @ApiImplicitParam(name = "priceMin", value = "最低价,仅当sort_order = price_max_min 时生效", required = false, dataType = "Float"),
            @ApiImplicitParam(name = "globlFieldId", value = "全球购场馆ID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "显示条数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "type", value = "附加参数", required = false, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/commodity/get_list_info", method = RequestMethod.GET)
    public String show(int pageNo, int pageSize, Integer primaryClassificationID, Integer secondaryClassificationID, Integer thridClassificationID, String searchKey,
                       String sortOrder, Float priceMax, Float priceMin, Integer globlFieldId,Integer type, HttpServletRequest req, HttpServletResponse rps) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        rps.setContentType("application/json;charset=UTF-8");
        //查询列表数据
        String code = "2";
        String msg = "处理异常！";
        PageSupport<Commodity> commList = commodityDao.findCommodityByCondition(pageNo, pageSize, primaryClassificationID,
                secondaryClassificationID, thridClassificationID, searchKey, sortOrder, priceMax, priceMin, globlFieldId,type);
        if (commList != null) {
            code = "1";
            msg = "获取成功！";
        } else {
            code = "1";
            msg = "未查询到商品信息！";
        }
        return resultMsg.mapToJson(code, msg, commList.getResultData() == null ? null : commList.getResultData());
    }

    /**
     * 获取单个商品详细信息
     *
     * @param accessToken token
     * @param id          商品ID
     * @return
     */
    @ApiOperation(value = " 获取单个商品详细信息", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/commodity/one", method = RequestMethod.GET)
    public String one(String accessToken, Integer id) {
        UserInfo user = userDao.getUserByToken(accessToken);
        String code = "2";
        String msg = "处理异常！";
        Commodity comm = commodityDao.findCommodity(id);
        Hibernate.initialize(comm.getCategories());
        comm.setCommodityHits(comm.getCommodityHits() + 1);
        comm = commodityDao.saveOrUpdate(comm);
        if (comm != null) {
            code = "1";
            msg = "获取成功！";
            if (user != null) {
                List<BrowseLog> commodityList = browseLogDAO.getUserByUidAndComm(user.getUid(), comm.getId());
                if (commodityList.size() == 0) {
                    BrowseLog bs = new BrowseLog();
                    bs.setClass_id(comm.getSpeciesId());
                    bs.setCommodity_id(comm.getId());
                    bs.setDate(new Date());
                    bs.setName(comm.getCommodityName());
                    bs.setPic_url(comm.getCommodityPicLarge());
                    bs.setCommodityPriceShop(comm.getCommodityPriceShop());
                    bs.setCommodityPriceUser(comm.getCommodityPriceUser());
                    bs.setTitle(comm.getCommodityName());
                    bs.setUid(user.getUid());
                    browseLogDAO.saveOrUpdate(bs);
                }
            }
        } else {
            code = "1";
            msg = "未查询到商品信息！";
        }
        return resultMsg.mapToJson(code, msg, comm);
    }

}
